package com.cxxx.guns.modular.backend.service.impl;

import com.cxxx.guns.core.base.tips.SuccessTip;
import com.cxxx.guns.core.common.exception.BizExceptionEnum;
import com.cxxx.guns.core.exception.GunsException;
import com.cxxx.guns.core.shiro.ShiroKit;
import com.cxxx.guns.core.util.ToolUtil;
import com.cxxx.guns.modular.backend.model.News;
import com.cxxx.guns.modular.backend.dao.NewsMapper;
import com.cxxx.guns.modular.backend.service.INewsService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.cxxx.guns.modular.system.model.User;
import com.cxxx.guns.modular.system.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 新闻表
 服务实现类
 * </p>
 *
 * @author cczhang
 * @since 2018-09-04
 */
@Service
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements INewsService {

    @Autowired
    private IUserService userService;

    private static SuccessTip SUCCESS_TIP = new SuccessTip();
    @Override
    public Object addNews(News news) {
        Integer userId = ShiroKit.getUser().getId();
        if (ToolUtil.isEmpty(userId)) {
            throw new GunsException(BizExceptionEnum.REQUEST_NULL);
        }
        User user = this.userService.selectById(userId);
        news.setUserId(userId);
        if(ToolUtil.isEmpty(user.getName())){
            news.setAuthor("匿名");
        }else {
            news.setAuthor(user.getName());
        }
        this.baseMapper.insert(news);
        return SUCCESS_TIP;
    }

    @Override
    public List<News> getList(String title, Integer model) {
        return this.baseMapper.getList(title, model);
    }

    @Override
    public List<News> getListIndex(int model) {
        return this.baseMapper.getNewsIndex(model);
    }

    @Override
    public News selectNewsPrevious(int status,Date date) {
        return this.baseMapper.selctNewsPrevious(status,date);
    }

    @Override
    public News selectNewsNext(int status,Date date) {
        return this.baseMapper.selectNewsNext(status,date);
    }
}
